﻿/// <reference path="jquery-1.10.2.min.js" />
;
var Ajax = (function () {
    var _xhr = new XMLHttpRequest();
    var _ajax = function (json) {
        if (_xhr.readyState > 1 && _xhr.readyState < 4) return;
        var options = {
            url: json.url ? json.url : "",
            type: json.type ? json.type.toUpperCase() : "GET",
            data: json.data ? json.data : null,
            dataType: json.dataType ? json.dataType : "",
            async: json.async ? json.async : true,
            success: json.success ? json.success : function (data) { },
            failure: json.failure ? json.failure : function (data) { }
        };
        if (options.type == "GET") {
            options.url = _makeGetRequestUrl(options.url, options.data);
        }
        _xhr.open(options.type, options.url, options.async);
        _xhr.onreadystatechange = function () {
            if (_xhr.readyState > 3) {
                if (_xhr.status == 200) {
                    options.success(_xhr.responseText);
                } else {
                    options.failure(_xhr.responseText);
                }
            }
        }
        if (options.type == "POST") {
            var formData = _makeQueryString(options.data);
            _xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            _xhr.send(formData);
        } else {
            _xhr.send();
        }
    }
    var _makeGetRequestUrl = function (url, data) {
        if (data == null) return url;
        var params = /\?/.test(url) ? "&" : "?";
        params += _makeQueryString(data)
        return url + params;
    }
    var _makeQueryString = function (data) {
        var params = "";
        var keys = new Array();
        for (var key in data) {
            keys.push(key);
        }
        params += keys[0] + "=" + data[keys[0]];
        for (var i = 1; i < keys.length; i++) {
            params += "&" + keys[i] + "=" + encodeURIComponent(data[keys[i]]);
        }
        return params;
    }
    var _get = function (url, data, success) {
        if (typeof (data) == "function") {
            success = data;
            data = null;
        }
        _ajax({
            url: url,
            type: "GET",
            data: data,
            success: success
        });
    }
    var _post = function (url, data, success) {
        if (typeof (data) == "function") {
            success = data;
            data = null;
        }
        _ajax({
            url: url,
            type: "POST",
            data: data,
            success: success
        });
    }
    var _getJSON = function (url, data, success) {
        _ajax({
            url: url,
            type: "GET",
            data: data,
            success: function (responseText) {
                try {
                    if (responseText.length > 1) {
                        success(JSON.parse(responseText));
                    }
                } catch (e) {
                    alert(e.message);
                }
            }
        });
    }
    var _reload = function () {
        _redirect(window.location.href);
    }
    var _redirect = function (url) {
        _get(
            url,
            function (data) {
                data = data.replace(/\s{2,}/g, "");
                var head = /<head.*?head>/i.exec(data)[0];
                var body = /<body.*?body>/i.exec(data)[0];
                var pageload = body.match(/<script[^<]*id=.pageload.*?script>/);
                var beforeAttrs = $("body")[0].attributes;
                var afterAttrs = body.match(/<body\s*([^>]*)>/)[1].match(/.+?=['"].*?['"]/g);
                for (var i = 0; i < beforeAttrs.length; i++) {
                    $("body").removeAttr(beforeAttrs[i].name);
                }
                if (afterAttrs != null) {
                    for (var i = 0; i < afterAttrs.length; i++) {
                        var tmpArray = afterAttrs[i].split("=");
                        $("body").attr(tmpArray[0], tmpArray[1].replace(/"/g, ""));
                    }
                }
                $("head").html(head.replace(/<head>|<\/head>/ig, ""));
                $("body").html(body.replace(/<body[^>]*>|<\/body>/ig, ""));
                $("body").append(pageload);
            }
        );
    }
    return {
        ajax: _ajax,
        get: _get,
        getJSON: _getJSON,
        post: _post,
        setUrlParams: _makeGetRequestUrl,
        reload: _reload,
        redirect: _redirect
    }
})();